package com.zhangsun.system.controller;

import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.UserEntity;
import com.zhangsun.system.service.UserService;
import com.zhangsun.system.vo.user.UserDisabledVo;
import com.zhangsun.system.vo.user.UserEditVo;
import com.zhangsun.system.vo.user.UserPageVo;
import com.zhangsun.system.vo.user.UserUpdatePasswordVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户
     *
     * @param vo 条件
     * @return 结果
     */
    @GetMapping("/findAll")
    public ResponseData findAll(UserPageVo vo) {
        Object result = userService.findAll(vo);
        return ResponseUtil.success(result);
    }

    /**
     * 添加用户
     *
     * @param vo 用户
     * @return 结果
     */
    @PostMapping("/create")
    @RequiresPermissions("system:user:edit")
    public ResponseData create(@Validated UserEditVo vo) {
        userService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 用户详情
     *
     * @param id 用户id
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:user:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        UserEntity userEntity = userService.findById(id);
        return ResponseUtil.success(userEntity);
    }

    /**
     * 更新用户
     *
     * @param id 用户id
     * @param vo 用户
     * @return 结果
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:user:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated UserEditVo vo) {
        userService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:user:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        userService.delete(id);
        return ResponseUtil.success();
    }

    /**
     * 更新密码
     *
     * @param id 用户id
     * @param vo 密码
     * @return 结果
     */
    @PutMapping("/updatePassword/{id}")
    @RequiresPermissions("system:user:updatePassword")
    public ResponseData updatePassword(@NotBlank @PathVariable String id, @Validated UserUpdatePasswordVo vo) {
        userService.updatePassword(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 禁用启用用户
     *
     * @param id 用户id
     * @param vo 状态
     * @return 结果
     */
    @PutMapping("/disabled/{id}")
    @RequiresPermissions("system:user:disabled")
    public ResponseData disabledUser(@NotBlank @PathVariable String id, @Validated UserDisabledVo vo) {
        userService.updateDisabled(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 初始化管理员账户
     *
     * @return 成功
     */
    @RequestMapping("/createAdmin")
    public ResponseData createAdmin() {
        userService.createAdmin();
        return ResponseUtil.success();
    }

}
